﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jnlamp.Web.api
{
    public partial class state_now : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["up"]))
                {
                    string[] s = Request["up"].Split('|') ;

                    if (s.Length == 6)
                    {
                        jnlamp.BLL.scdstatus sm = new BLL.scdstatus ();

                        string gpris_id = s[0];

                        List<jnlamp.Model.scdstatus> ls = sm.GetModelList(" gprs_id ='" + gpris_id + "' ");

                        if (ls.Count == 1)
                        {
                            jnlamp.Model.scdstatus scdstatus = ls[0];

                            setEntity(s, scdstatus, sm);
                        }
                        else
                        {
                            setEntity(s, null, sm);
                        }
                    }
                }
            }
        }

        private static void setEntity(string[] s, jnlamp.Model.scdstatus scdstatus, jnlamp.BLL.scdstatus sm)
        {
            int ts = Convert.ToInt32(s[4]);

            int vf = 0;
            for (int i = 0; i < s[2].Length; i++)
            {
                //int t = Convert.ToInt32(s[2][i].ToString());
                vf += Convert.ToInt32(s[2][i].ToString());
            }
            for (int i = 0; i < s[3].Length; i++)
            {
                vf += Convert.ToInt32(s[3][i].ToString());
            }

            vf += ts;

            if (scdstatus != null)
            {
                if (vf == Convert.ToInt32(s[5]))//校验位检验
                {
                    scdstatus.gprs_id = s[0];
                    scdstatus.opt = s[1];
                    scdstatus.kdsj = s[2];
                    scdstatus.kdsc = s[3];
                    scdstatus.ts = ts;
                    scdstatus.vf = vf;
                    scdstatus.uptime = DateTime.Now;

                    sm.Update(scdstatus);
                }
            }
            else
            {
                if (vf == Convert.ToInt32(s[5]))//校验位检验
                {
                    scdstatus = new Model.scdstatus();
                    scdstatus.gprs_id = s[0];
                    scdstatus.opt = s[1];
                    scdstatus.kdsj = s[2];
                    scdstatus.kdsc = s[3];
                    scdstatus.ts = ts;
                    scdstatus.vf = vf;
                    scdstatus.uptime = DateTime.Now;

                    sm.Add(scdstatus);

                }
            }
            
        }
    }
}